﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;

namespace BLBTSystem
{
    /// <summary>
    /// UpLoad 的摘要说明
    /// </summary>
    public class UpLoad : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html;charset=utf-8";
            JavaScriptSerializer ser = new JavaScriptSerializer();
            MMM.Services.Response.OperationResult<string> obj = new MMM.Services.Response.OperationResult<string>();
            HttpPostedFile filePost = context.Request.Files["Filedata"];
            string fileName = Path.GetFileName(filePost.FileName);
            string fileExtions = Path.GetExtension(fileName).ToLower();
            if (fileExtions == ".jpg" || fileExtions == ".gif" || fileExtions == ".bmp" || fileExtions == ".xls")
            {
                string md5Name = MMM.Services.Expansion.CommonFunction.Commonfunction.GetStreamMD5(filePost.InputStream);
                string dieName = "/UpLoad/";
                string upFileName = dieName + md5Name + fileExtions;
                string fullPath = context.Server.MapPath(upFileName);
                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
                filePost.SaveAs(fullPath);

                obj.success = true;
                obj.ResponseStatus.Message = upFileName;
                context.Response.Write(ser.Serialize(obj));
            }
            else
            {
                obj.success = false;
                obj.ResponseStatus.Message = "请输入正确的数据文件";
                context.Response.Write(ser.Serialize(obj));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}